/**   
 * Copyright © 2016 北京之洲科技有限责任公司. All rights reserved.
 * 
 * @Title: BBSCrawl.java 
 * @Prject: carfactory
 * @Package: com.zizo.autohome.bbs.crawl 
 * @Description: 
 * @author: Alexis  
 * @email heyan0227@outlook.com
 * @date: 2016年7月26日 上午11:30:42 
 * @version: V1.0   
 */
package com.zizo.autohome.bbs.crawl;

import java.io.File;
import java.sql.Connection;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.regex.Matcher;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Document.OutputSettings;
import org.jsoup.nodes.Element;
import org.jsoup.parser.Parser;
import org.jsoup.safety.Whitelist;
import org.jsoup.select.Elements;

import com.zizo.autohome.bbs.bean.AutoHomeBBS;
import com.zizo.autohome.bbs.dao.BBSDAOOfAutoHome;
import com.zizo.core.Crawl;
import com.zizo.core.DAO;
import com.zizo.util.ConnectionPool;
import com.zizo.util.DocUtil;
import com.zizo.util.Util;

/**
 * @ClassName: BBSCrawl
 * @Description:
 * @author: Alexis
 * @date: 2016年7月26日 上午11:30:42
 */
public class BBSCrawlOfAutoHomeByFile extends Crawl {
	BBSDAOOfAutoHome bbsdao = (BBSDAOOfAutoHome) this.dao;

	/**
	 * @Title:BBSCrawl
	 * @Description:
	 * @param id
	 * @param dao
	 */
	public BBSCrawlOfAutoHomeByFile(String id, DAO dao) {
		super(id, dao);
		// TODO Auto-generated constructor stub
	}

	/*
	 * (non Javadoc)
	 * 
	 * @Title: getPages
	 * 
	 * @Description:
	 * 
	 * @param doc
	 * 
	 * @param selectStr
	 * 
	 * @return1
	 * 
	 * @see com.zizo.core.CrawlInterface#getPages(org.jsoup.nodes.Document,
	 * java.lang.String)
	 */
	@Override
	public int getPages(Document doc, String selectStr) {
		return 0;
	}

	/*
	 * (non Javadoc)
	 * 
	 * @Title: getItems
	 * 
	 * @Description:
	 * 
	 * @param url
	 * 
	 * @see com.zizo.core.CrawlInterface#getItems(java.lang.String)
	 */
	@Override
	public void getItems(String url) {
	}

	/*
	 * (non Javadoc)
	 * 
	 * @Title: crawlContent
	 * 
	 * @Description:
	 * 
	 * @param url
	 * 
	 * @see com.zizo.core.CrawlInterface#crawlContent(java.lang.String)
	 */
	@Override
	public void crawlContent(String pathname) {
		try {
			Document doc = DocUtil.getDocByFile(pathname);
			if (doc == null) {
				return;
			}
			if (doc.select("h1.rtitle").text().contains("帖子被删除（或屏蔽）！")) {
				return;
			}
			String ppidTemp = doc.select("a#a_bbsname").attr("href");
			Elements clubEle = doc.select("a#xid-owner");
			String clubID = clubEle.attr("href");
			int bbsID = Integer.parseInt(clubID.split("-")[3]);// 论坛ID
			int question = doc.select("div.qa-rtitle").size() > 0 ? 1 : 0;// 是否问答
																			// 0--否
																			// 1--是
			String title = "";// 标题
			if (question == 1) {
				title = doc.select("div.qa-maxtitle").text();
			} else {
				title = doc.select("div.maxtitle").text();// 标题
			}
			int essence = doc.select("span.pngfix").size() > 0 ? 1 : 0;// 是否精华
			String nr = doc.select("div.conttxt div.w740").html();
			OutputSettings settings = new OutputSettings();
			settings.prettyPrint(false);
			String topic= Parser.unescapeEntities(Jsoup.clean(title, "", Whitelist.none(), settings),false).trim();//处理后标题
			String content = Parser.unescapeEntities(Jsoup.clean(nr, "", Whitelist.none(), settings),false).trim();// 评论内容
			String topTopic = doc.select("div.rtopcon span").text();// 菜单栏顶部的字符串，发表时间，来自于
			String postTime = "2000-10-1 00:00:00";// 发帖时间
			if (topTopic.contains("发表于 ")) {
				postTime = Util.getMatched(topTopic, "(.*发表于 )(\\d{4}-\\d{1,}-\\d{1,} \\d{2}:\\d{2}:\\d{2})(.*)")
						.group(2);
			}
			String hit = doc.select("#x-views").text();
			int hitCount = 0;
			if (!hit.equals("")) {
				hitCount = Integer.parseInt(hit);// 点击数
			}
			String rep = doc.select("#x-replys").text();
			int replyCount = 0;
			if (!hit.equals("")) {
				replyCount = Integer.parseInt(rep);// 点击数
			}
			int userID = Integer.parseInt(doc.select("div#maxwrap-maintopic div.outer-section").first().attr("uid"));// 用户ID
			String userName = doc.select("div#maxwrap-maintopic li.txtcenter a.c01439a").text();// 用户名称
			int essenceCount = 0;// 用户发帖中的精华帖子数
			int postCount = 0;// 发帖数
			int repCount = 0;// 该发帖用户所有回复帖子的数量
			String registerDate = "2000-01-01";// 注册日期,默认2000-01-01为没有注册日期
			String province = "";// 用户的省份
			String city = "";// 用户地市
			int certificationCars = 0;// 是否为认证车主
			Elements userInfo = doc.select("div#maxwrap-maintopic ul.leftlist li:not(.leftimgs)");
			for (Element li : userInfo) {
				if (li.text().contains("精华")) {
					essenceCount = Integer.parseInt(Util.getMatched(li.text(), "(.*精华：)(\\d{1,})(帖.*)").group(2));
				} else if (li.text().contains("帖子：")) {
					Matcher matcher = Util.getMatched(li.text(), "(帖子：)(\\d{1,})(帖.*|\\S{0,})(\\d{1,})(回)");
					postCount = Integer.parseInt(matcher.group(2));
					repCount = Integer.parseInt(matcher.group(4));
				} else if (li.text().contains("注册：")) {
					registerDate = li.text().split("注册：")[1];
				} else if (li.text().contains("来自：")) {
					String tempStr = li.text().substring(3);// 去掉来自：取后面的字符串
					String[] location = tempStr.split(" ");
					province = location[0];
					city = location.length > 1 ? location[1] : "";
				} else if (li.text().contains("爱车：")) {
					Elements as = userInfo.select("a");// 认证车子
					for (Element a : as) {
						if (a.attr("href").contains("/bbs/forum-") && a.attr("href").contains("#pvareaid=103121")) {
							certificationCars = 1;
							break;
						}
					}
				}
			}
			String clientFrom = "";// 来自于那个客户端，安卓，iPhone。。
			if (topTopic.contains("来自 ")) {
				clientFrom = Util.getMatched(topTopic, "(.*来自 )(.*)").group(2);
			}
			AutoHomeBBS autoHomeBBS = new AutoHomeBBS(bbsID, ppidTemp.split("-")[2], Util.removeFullWidth(topic),
					question, essence, Util.removeFullWidth(content), postTime, hitCount, replyCount, userID, userName,
					essenceCount, postCount, repCount, registerDate, Util.removeFullWidth(province),
					Util.removeFullWidth(city), certificationCars, clientFrom, Util.getCurrentDateTime());
			String table = clubID.split("-")[1] + "_" + ppidTemp.split("-")[2];
			bbsdao.createTable("bbs_" + table);// 验证是否有该品牌ID的表，没有则创建，有则跳过
			bbsdao.insertData(autoHomeBBS, "bbs_" + table);// 插入BBS
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(this.id+"有问题=========================");
		}
	}

	/*
	 * (non Javadoc)
	 * 
	 * @Title: run
	 * 
	 * @Description:
	 * 
	 * @see java.lang.Runnable#run()
	 */
	@Override
	public void run() {
		File[] files = new File(this.id).listFiles();
		for (File file : files) {
			crawlContent(file.getAbsolutePath());
		}
// 	crawlContent(this.id);
	}

	public static void main(String[] args) {
		Connection currConn = ConnectionPool.getConnection("autohome_bbs", "localhost", "3306");
		BBSDAOOfAutoHome bbsdao = new BBSDAOOfAutoHome(currConn);
		ExecutorService fix = Executors.newFixedThreadPool(15);
		File[] files = new File("/mnt/mirror/autohome/bbs").listFiles();
		for (File file : files) {
			fix.execute(new BBSCrawlOfAutoHomeByFile(file.getAbsolutePath(), bbsdao));
		}
	}
}
